package com.explore.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.explore.demo.VO.CompanyInfoVO;
import com.explore.demo.VO.RecruitmentSearchVO;
import com.explore.demo.VO.RecruitmentVO;
import com.explore.demo.VO.SearchDataVO;
import com.explore.demo.common.ServerResponse;
import com.explore.demo.mapper.*;
import com.explore.demo.pojo.*;
import com.explore.demo.service.IndexInfoService;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.web.config.QuerydslWebConfiguration;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author le
 */
@Service
public class IndexInfoServiceImpl implements IndexInfoService
{

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private RecruitCategoryMapper recruitCategoryMapper;

    @Resource
    private RecruitmentMapper recruitmentMapper;

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private RowViewMapper rowViewMapper;

    @Override
    public ServerResponse getAllRecruitmentInfo(@NotNull String categoryName)
    {
        List<RecruitmentVO> res = new ArrayList<>();
        List<Recruitment> allRecruitmentInfo = recruitmentMapper.getAllRecruitmentInfo(categoryName);
        for (Recruitment tmp : allRecruitmentInfo){
            RecruitmentVO recruitmentVO = new RecruitmentVO();
            List<String> name = companyMapper.getCategoryName(tmp.getCompanyId());
            QueryWrapper<Company> wrapper = new QueryWrapper<>();
            wrapper.eq("id", tmp.getCompanyId());
            Company company = companyMapper.selectOne(wrapper);
            recruitmentVO
                    .setRecruitmentId(tmp.getId())
                    .setJob(tmp.getTitle())
                    .setWorkplace(tmp.getWorkCity())
                    .setSalaryUp(tmp.getSalaryUp())
                    .setSalaryDown(tmp.getSalaryDown())
                    .setEducation(tmp.getEducation())
                    .setExperience(tmp.getExperience())
                    .setLogo(company.getCompanyLogo())
                    .setCompanyName(company.getCompanyName())
                    .setCompanyCategory(name)
                    .setStatus(company.getStatus());
            res.add(recruitmentVO);
        }
        return ServerResponse.createBySuccessMessage("success", 200, res);
    }

    @Override
    public ServerResponse getAllCompanyInfo(@NotNull String categoryName)
    {
        List<CompanyInfoVO> res = new ArrayList<>();
        List<Company> allCompanyInfo = companyMapper.getAllCompanyInfo(categoryName);
        for (Company tmp : allCompanyInfo){
            CompanyInfoVO companyInfoVO = new CompanyInfoVO();
            companyInfoVO
                    .setLogo(tmp.getCompanyLogo())
                    .setCompanyName(tmp.getCompanyName())
                    .setStatus(tmp.getStatus())
                    .setCompanyCategory(companyMapper.getCategoryName(tmp.getId()))
                    .setRecruitCounts(companyMapper.getNowRecruitmentNum(tmp.getId()));
            res.add(companyInfoVO);
        }
        return ServerResponse.createBySuccessMessage("success", 200, res);
    }

    @Override
    public ServerResponse getTopRecruitmentInfo()
    {
        List<RecruitmentVO> res = new ArrayList<>();
        List<Recruitment> topRecruitmentList = recruitmentMapper.getTopRecruitmentList();
        for (int i = topRecruitmentList.size()-1;i >= 0;i--){
            Recruitment tmp = topRecruitmentList.get(i);
            RecruitmentVO recruitmentVO = new RecruitmentVO();
            List<String> name = companyMapper.getCategoryName(tmp.getCompanyId());
            QueryWrapper<Company> wrapper = new QueryWrapper<>();
            wrapper.eq("id", tmp.getCompanyId());
            Company company = companyMapper.selectOne(wrapper);
            recruitmentVO
                    .setRecruitmentId(tmp.getId())
                    .setJob(tmp.getTitle())
                    .setWorkplace(tmp.getWorkCity())
                    .setSalaryUp(tmp.getSalaryUp())
                    .setSalaryDown(tmp.getSalaryDown())
                    .setEducation(tmp.getEducation())
                    .setExperience(tmp.getExperience())
                    .setLogo(company.getCompanyLogo())
                    .setCompanyName(company.getCompanyName())
                    .setCompanyCategory(name)
                    .setStatus(company.getStatus());
            res.add(recruitmentVO);
        }
        return ServerResponse.createBySuccessMessage("success", 200, res);
    }

    @Override
    public ServerResponse getTopCompanyInfo()
    {
        List<CompanyInfoVO> res = new ArrayList<>();
        List<Company> topCompany = companyMapper.getTopCompany();
        for (Company tmp : topCompany){
            QueryWrapper<RowView> wrapper = new QueryWrapper<>();
            wrapper.eq("company_id", tmp.getId());
            List<RowView> rowViews = rowViewMapper.selectList(wrapper);
            CompanyInfoVO companyInfoVO = new CompanyInfoVO();
            companyInfoVO.setLogo(tmp.getCompanyLogo())
                         .setCompanyName(tmp.getCompanyName())
                         .setStatus(tmp.getStatus())
                         .setCompanyCategory(companyMapper.getCategoryName(tmp.getId()))
                         .setRecruitCounts(rowViews.size());
            res.add(companyInfoVO);
        }

        return ServerResponse.createBySuccessMessage("success", 200, res);
    }

    @Override
    public ServerResponse queryDataByCompanyOrRecruit(@NotNull String text)
    {
        SearchDataVO searchDataVO = new SearchDataVO();
        QueryWrapper<Company> wrapper = new QueryWrapper<>();
        wrapper
                .eq("company_name", text);
        Company company = companyMapper.selectOne(wrapper);
        if (company != null){
            CompanyInfoVO companyInfoVO = new CompanyInfoVO();
            companyInfoVO
                    .setLogo(company.getCompanyLogo())
                    .setCompanyName(company.getCompanyName())
                    .setStatus(company.getStatus())
                    .setCompanyCategory(companyMapper.getCategoryName(company.getId()))
                    .setRecruitCounts(companyMapper.getNowRecruitmentNum(company.getId()));
            searchDataVO.setCompanyInfoVO(companyInfoVO);
            List<RecruitmentSearchVO> recruitmentSearchVOList = new ArrayList<>();
            QueryWrapper<Recruitment> recruitmentQueryWrapper = new QueryWrapper<>();
            recruitmentQueryWrapper.eq("company_id", company.getId());
            List<Recruitment> list = recruitmentMapper.selectList(recruitmentQueryWrapper);
            for (Recruitment tmp : list){
                RecruitmentSearchVO recruitmentSearchVO = new RecruitmentSearchVO();
                recruitmentSearchVO.setTitle(tmp.getTitle())
                                   .setRecruitmentId(tmp.getId())
                                   .setWorkPlace(tmp.getWorkCity())
                                   .setExperience(tmp.getExperience())
                                   .setCreateDate(tmp.getCreateTime())
                                   .setSalaryUp(tmp.getSalaryUp())
                                   .setSalaryDown(tmp.getSalaryDown())
                                   .setEducation(tmp.getEducation())
                                   .setNeedNum(tmp.getNeedNum())
                                   .setRecruitCategory(recruitmentMapper.getAllCategory(tmp.getId()))
                                   .setWelfareCategory(recruitmentMapper.getAllWelfareCategory(tmp.getId()))
                                   .setCompanyName(company.getCompanyName())
                                   .setCompanyStatus(company.getStatus())
                                   .setCompanyLogo(company.getCompanyLogo())
                                   .setCompanyCategory(companyMapper.getCategoryName(company.getId()));
                recruitmentSearchVOList.add(recruitmentSearchVO);
            }
            searchDataVO.setCompanySearchVOList(recruitmentSearchVOList);
            return ServerResponse.createBySuccessMessage("success", 200, searchDataVO);
        }
        List<RecruitmentSearchVO> res = new ArrayList<>();
        searchDataVO.setCompanyInfoVO(null);
        QueryWrapper<Recruitment> recruitmentQueryWrapper = new QueryWrapper<>();
        recruitmentQueryWrapper.like("title", text);
        List<Recruitment> recruitmentList = recruitmentMapper.selectList(recruitmentQueryWrapper);
        for (Recruitment tmp : recruitmentList){
            RecruitmentSearchVO recruitmentSearchVO = new RecruitmentSearchVO();
            QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();
            companyQueryWrapper.eq("id", tmp.getCompanyId());
            Company one = companyMapper.selectOne(companyQueryWrapper);
            recruitmentSearchVO.setTitle(tmp.getTitle())
                               .setRecruitmentId(tmp.getId())
                               .setWorkPlace(tmp.getWorkCity())
                               .setExperience(tmp.getExperience())
                               .setCreateDate(tmp.getCreateTime())
                               .setSalaryUp(tmp.getSalaryUp())
                               .setSalaryDown(tmp.getSalaryDown())
                               .setEducation(tmp.getEducation())
                               .setNeedNum(tmp.getNeedNum())
                               .setRecruitCategory(recruitmentMapper.getAllCategory(tmp.getId()))
                               .setWelfareCategory(recruitmentMapper.getAllWelfareCategory(tmp.getId()))
                               .setCompanyName(one.getCompanyName())
                               .setCompanyStatus(one.getStatus())
                               .setCompanyLogo(one.getCompanyLogo())
                               .setCompanyCategory(companyMapper.getCategoryName(one.getId()));
            res.add(recruitmentSearchVO);
        }
        searchDataVO.setCompanySearchVOList(res);
        return ServerResponse.createBySuccessMessage("success", 200, searchDataVO);
    }

    @Override
    public ServerResponse queryByPlace(@NotNull String place)
    {
        SearchDataVO searchDataVO = new SearchDataVO();
        List<RecruitmentSearchVO> res = new ArrayList<>();
        searchDataVO.setCompanyInfoVO(null);
        List<Recruitment> recruitmentList = recruitmentMapper.queryRecruitmentByPlace(place);
        for (Recruitment tmp : recruitmentList){
            RecruitmentSearchVO recruitmentSearchVO = new RecruitmentSearchVO();
            QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();
            companyQueryWrapper.eq("id", tmp.getCompanyId());
            Company one = companyMapper.selectOne(companyQueryWrapper);
            recruitmentSearchVO.setRecruitmentId(tmp.getId())
                               .setTitle(tmp.getTitle())
                               .setWorkPlace(tmp.getWorkCity())
                               .setCreateDate(tmp.getCreateTime())
                               .setExperience(tmp.getExperience())
                               .setSalaryUp(tmp.getSalaryUp())
                               .setSalaryDown(tmp.getSalaryDown())
                               .setEducation(tmp.getEducation())
                               .setNeedNum(tmp.getNeedNum())
                               .setRecruitCategory(recruitmentMapper.getAllCategory(tmp.getId()))
                               .setCompanyName(one.getCompanyName())
                               .setCompanyStatus(one.getStatus())
                               .setCompanyLogo(one.getCompanyLogo())
                               .setCompanyCategory(companyMapper.getCategoryName(one.getId()));
            res.add(recruitmentSearchVO);
        }
        searchDataVO.setCompanySearchVOList(res);
        return ServerResponse.createBySuccessMessage("success", 200, searchDataVO);
    }

    @Override
    public ServerResponse getSimilarRecruitmentByCategoryName(Integer recruitmentId)
    {
        HashSet<Recruitment> set = new HashSet<>();
        List<RecruitmentSearchVO> res = new ArrayList<>();
        // 根据对应的职位的分类查询的
        List<String> categoryName = categoryMapper.getCategoryName(recruitmentId);
        for (String tmp : categoryName){
            List<Recruitment> info = recruitmentMapper.getAllRecruitmentInfo(tmp);
            for (Recruitment temp : info){
                set.add(temp);
            }
        }
        // 根据职位的名称查询
        QueryWrapper<Recruitment> wrapper = new QueryWrapper<>();
        wrapper.like("title", recruitmentMapper.selectOne(new QueryWrapper<Recruitment>().eq("id", recruitmentId)).getTitle());
        List<Recruitment> recruitments = recruitmentMapper.selectList(wrapper);
        for (Recruitment tmp : recruitments){
            set.add(tmp);
        }
        Iterator<Recruitment> iterator = set.iterator();
        while(iterator.hasNext()){
            Recruitment next = iterator.next();
            RecruitmentSearchVO recruitmentSearchVO = new RecruitmentSearchVO();
            QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();
            companyQueryWrapper.eq("id", next.getCompanyId());
            Company one = companyMapper.selectOne(companyQueryWrapper);
            recruitmentSearchVO.setRecruitmentId(next.getId())
                               .setTitle(next.getTitle())
                               .setWorkPlace(next.getWorkCity())
                               .setCreateDate(next.getCreateTime())
                               .setExperience(next.getExperience())
                               .setSalaryUp(next.getSalaryUp())
                               .setSalaryDown(next.getSalaryDown())
                               .setEducation(next.getEducation())
                               .setNeedNum(next.getNeedNum())
                               .setRecruitCategory(recruitmentMapper.getAllCategory(next.getId()))
                               .setCompanyName(one.getCompanyName())
                               .setCompanyStatus(one.getStatus())
                               .setCompanyLogo(one.getCompanyLogo())
                               .setWelfareCategory(recruitmentMapper.getAllWelfareCategory(next.getId()))
                               .setCompanyCategory(companyMapper.getCategoryName(next.getId()));
            res.add(recruitmentSearchVO);
        }

        return ServerResponse.createBySuccessMessage("success", 200, res);
    }

    @Override
    public ServerResponse getMoreRecruitmentInfo(@NotNull String categoryName)
    {
        List<RecruitmentSearchVO> res = new ArrayList<>();
        List<Recruitment> recruitmentList = recruitmentMapper.getAllRecruitmentInfo(categoryName);
        for (Recruitment tmp : recruitmentList){
            RecruitmentSearchVO searchVO = new RecruitmentSearchVO();
            QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();
            companyQueryWrapper.eq("id", tmp.getCompanyId());
            Company one = companyMapper.selectOne(companyQueryWrapper);
            searchVO.setRecruitmentId(tmp.getId())
                               .setTitle(tmp.getTitle())
                               .setWorkPlace(tmp.getWorkCity())
                               .setCreateDate(tmp.getCreateTime())
                               .setExperience(tmp.getExperience())
                               .setSalaryUp(tmp.getSalaryUp())
                               .setSalaryDown(tmp.getSalaryDown())
                               .setEducation(tmp.getEducation())
                               .setNeedNum(tmp.getNeedNum())
                               .setRecruitCategory(recruitmentMapper.getAllCategory(tmp.getId()))
                               .setCompanyName(one.getCompanyName())
                               .setCompanyStatus(one.getStatus())
                               .setCompanyLogo(one.getCompanyLogo())
                               .setWelfareCategory(recruitmentMapper.getAllWelfareCategory(tmp.getId()))
                               .setCompanyCategory(companyMapper.getCategoryName(one.getId()));
            res.add(searchVO);
        }
        return ServerResponse.createBySuccessMessage("success", 200, res);
    }


}
